<?php

/**
 * DtGroundOrder form.
 *
 * @package    DUYTAN
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DtGroundOrderForm extends BaseDtGroundOrderForm
{
  static $status_values = array(
	''		=>	'Tất cả',
  	'0'		=>	'Khởi tạo',
	'1'		=>	'Đã thanh toán',
	'2'		=>	'Quá hạn',
	'3'		=>	'Huỷ',
  );

  public function configure()
  {
	$action = sfContext::getInstance()->getActionName();
	$this->setWidgets(array(
		'customer_id'					=> new sfWidgetFormChoice(array('choices' => $this->getCustomerList())),
		'reserved_date'					=> new sfWidgetFormInput(array(), array('onChange' => 'loadSchedule()')),
		'ground_id'						=> new sfWidgetFormChoice(
				array('choices' => $this->getGroundList()),
				array('onChange' => 'loadSchedule()')
		),
		'schedule'						=> new sfWidgetFormChoice(
				array('choices' => array()),
				array('onChange' => 'loadCost()')
		),
		'status'						=> new sfWidgetFormChoice(array('choices' => self::$status_values)),
		'note'							=> new sfWidgetFormTextarea(),
		'amount'						=> new sfWidgetFormInput(),
		'is_for_student'				=> new sfWidgetFormInputCheckbox(),
	));
	if ($action == 'edit')
		$this->setValidators(array(
			'customer_id'				=> new sfValidatorChoice(
				array('required' => true, 'choices' => array_keys($this->getCustomerList())),
				array('required' => 'Hãy chọn khách hàng')
			),
			'reserved_date'				=> new sfValidatorString(
				array('required' => true), 
				array('required' => 'Hãy chọn ngày đặt sân')),
			'ground_id'					=> new sfValidatorChoice(
				array(	'required' => true, 
						'choices'  => array_keys($this->getGroundList())), 
				array(	'required' => 'Hãy chọn sân')),
			'status'					=> new sfValidatorChoice(array('choices' => array_keys(self::$status_values))),
			'schedule'					=> new sfValidatorString(
				array('required' => true),
				array('required' => 'Hãy chọn giờ đặt sân')
			),
			'note'						=> new sfValidatorString(array('required' => false)),
			'amount'					=> new sfValidatorInteger(array('required' => false)),
		));
	$this->widgetSchema->setLabels(array(
		'customer_id'					=> 'Khách hàng',
		'ground_id'						=> 'Tên sân',
		'reserved_date'					=> 'Ngày đặt',
		'schedule'						=> 'Giờ đặt',
		'status'						=> 'Trạng thái',
		'note'							=> 'Ghi chú',
		'amount'						=> 'Đã thanh toán',
		'is_for_student'				=> 'Dành cho sinh viên',
	));
	$this->widgetSchema->setNameFormat('ground_order[%s]');
  }


  private function getCustomerList()
  {
	$list = array('' => '');
	$q = Doctrine_Query::create()
		->select('c.id, c.name, c.phone_number')
		->from('DtCustomer c');
	$customers = $q->fetchArray();
	foreach ($customers as $customer)
		$list[$customer['id']] = $customer['name'].' - '.$customer['phone_number'];
	return $list;
  }


  private function getGroundList()
  {
	$list = array('' => '');
	$q = Doctrine_Query::create()
		->select('g.id, g.name')
		->from('DtGround g');
	$grounds = $q->fetchArray();
	foreach ($grounds as $ground)
		$list[$ground['id']] = $ground['name'];
	return $list;
  }

}
